var EventEmitter = require('events').EventEmitter;
var io = require('socket.io');
var http = require('http');
var hashlib = require('hashlib2');
var fs = require('fs');
var util = require('util');

var ops = {
  server: http.createServer(function(req, res) {
    var dir = __dirname + '/..';
    if(req.url == '/') {
      dir += '/test.html';
    } else {
      dir += req.url;
    }
    fs.readFile(dir,
    function (err, data) {
      if (err) {
	res.writeHead(500);
	return res.end('Error loading '+req.url);
      }

      res.writeHead(200);
      res.end(data);
    });
  }),
  url: {
    mobile: 'mobile',
    browser: 'browser'
  },
  port: 8080
};

function isEmpty(obj) {
  for(var i in obj) {
    return false;
  }
  return true;
}

exports.init = function(v_ops) {
  
  var element = new EventEmitter();
  
  for(var i in v_ops) {
    ops[i] = v_ops[i];
  }
  
  element.namespaces = {};
  
  var getUnique = function () {
    var rand = hashlib.md5(Math.random());
    if(element.namespaces[rand]) {
      rand = getUnique();
    }
    return rand;
  };
  
  io = io.listen(ops.server);
  ops.server.listen(ops.port);
  
  element.mobile = io.of('/'+ops.url.mobile);
  element.browser = io.of('/'+ops.url.browser);
  
  element.fakeMobile = function() {
    var id = element.newMobile();
    return element.namespaces[id];
  };
  
  element.newMobile = function(){
    var id = getUnique();
    element.namespaces[id]={
      id: id,
      emit: function() {
	this.mobile.emit.apply(this.mobile, arguments);
	this.browser.emit.apply(this.browser, arguments);
      }, 
      on: function() {
	this.mobile.on.apply(this.mobile, arguments);
	this.browser.on.apply(this.browser, arguments);
      },
      mobile: {
	emit: function() {
	  for(var i=0; i<this.sockets.length; i++) {
	    this.sockets[i].emit.apply(this.sockets[i], arguments);
	  }
	},
	on: function() {
	  for(var i=0; i<this.sockets.length; i++) {
	    this.sockets[i].on.apply(this.sockets[i], arguments);
	  }
	},
	sockets: []
      }, 
      browser: {
	emit: function() {
	  for(var i=0; i<this.sockets.length; i++) {
	    this.sockets[i].emit.apply(this.sockets[i], arguments);
	  }
	},
	on: function() {
	  for(var i=0; i<this.sockets.length; i++) {
	    this.sockets[i].on.apply(this.sockets[i], arguments);
	  }
	},
	sockets: []
      }
    };
    io.of('/'+id+'/'+ops.url.browser).on('connection', function(socket) {
      element.namespaces[id].browser.sockets.push(socket);
      if(element.namespaces[id].mobile.sockets.length) 
	element.emit('connection', element.namespaces[id]);
    });
    element.browser.emit('createMobile', id);
    return id;
  };
  
  element.mobile.on('connection', function(socket) {
    socket.on('requestMobile', function() {
      console.log('received request for new mobile');
      var id = element.newMobile();
      //console.log(util.inspect(io.sockets.manager.rooms));
      io.of('/'+id+'/'+ops.url.mobile).on('connection', function(socket) {
	element.namespaces[id].mobile.sockets.push(socket);
	if(element.namespaces[id].browser.sockets.length) 
	  element.emit('connection', element.namespaces[id]);
      });
      socket.emit('createMobile', id);
    });
    
  });
  
  element.browser.on('connection', function(socket) {
    for(var i in element.namespaces) {
      socket.emit('createMobile', element.namespaces[i].id);
    }
  });
  
  return element;
};